import plotly
import plotly.io as pio
pio.renderers.default = "notebook+pdf"
import pandas as pd
import seaborn as sns
from pandas.plotting import scatter_matrix
import matplotlib.pyplot as plt
from matplotlib import font_manager as font_manager
from datetime import datetime,timedelta
import numpy as np
import plotly.express as px
import plotly.figure_factory as ff
import plotly.graph_objects as go
df=pd.DataFrame({'Years at work':[1,2,3,4],'income':[6500,10000,12000,14000]})
df.head()
| Years at work | income | |
|---|---|---|
| 0 | 1 | 6500 |
| 1 | 2 | 10000 |
| 2 | 3 | 12000 |
| 3 | 4 | 14000 |
#simple line using plotly
fig=px.line(df,x='Years at work',y='income',title='Income by No of Years at Work')
fig.show()
#simple line using seaborn
sns.set_style("darkgrid")
fig, ax = plt.subplots(figsize=(20,7))
s=sns.lineplot(data=df, x="Years at work", y="income")
s.set_title("Income by No of Years at Work")
Text(0.5, 1.0, 'Income by No of Years at Work')
stud=['yasmeen','dalia','eman','sama']
math=[89,70,90,98]
science=[78,96,67,87]
programming=[81,87,92,99]
database_subject=[91,93,67,89]
#Multiple line Using Plotly
fig=go.Figure()
fig.add_trace(go.Scatter(x=stud,y=math,name="Maths Marks",line=dict(color='blue',width=4)))
fig.add_trace(go.Scatter(x=stud,y=science,name="science Marks",line=dict(color='red',width=4)))
fig.add_trace(go.Scatter(x=stud,y=programming,name="Programming Marks",line=dict(color='green',width=4,dash='dash')))
fig.add_trace(go.Scatter(x=stud,y=database_subject,name="Database Marks",line=dict(color='purple',width=4,dash='dot')))
fig.update_layout(
title='Students Marks',xaxis_title='Students',yaxis_title='Marks',
title_font_size=20,
font_family="Courier New",
font_color="blue",
title_font_family="Times New Roman",
title_font_color="red",
legend_title_font_color="green",
font=dict(
family="Courier New, monospace",
size=20,
color="RebeccaPurple"
)
)
fig.show()
#Multiple line Using Seaborn
sns.set_style("ticks")
sns.set(font_scale = 1.5)
fig, ax = plt.subplots(figsize=(20,7))
ax= sns.lineplot(x=stud, y=math,label='Math',color='blue',linewidth=2)
ax1 = sns.lineplot(x=stud, y=science,label="Science",color='red',linewidth=2 )
ax2 = sns.lineplot(x=stud, y=programming,linestyle='--',label='Programming',color='green',linewidth=2)
ax3 = sns.lineplot(x=stud, y=database_subject,linestyle=':',label='Database',color='purple',linewidth=2)
ax.set_title("Students Marks")
ax.legend()
<matplotlib.legend.Legend at 0x1e3dd612b20>
df=sns.load_dataset('titanic')
df.head()
| survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
# bar plot using plotly
fig=px.bar(df,x='pclass',y='fare',color='sex',title='Passenger Class by fare and gender')
fig.show()
fig, ax1 = plt.subplots(figsize=(10, 10))
sns.barplot(x='pclass', y='fare', hue='sex', data=df, ax=ax1)
<AxesSubplot:xlabel='pclass', ylabel='fare'>
#Area using plotly
fig=px.area(df,x='sex',y='age',color='survived',title='Area plot for passenger status by gender and age ')
fig.show()
#Scatter stacked using plotly
stud=['yasmeen','samah','faten']
fig=go.Figure()
fig.add_trace(go.Scatter(x=stud,y=[40,10,20],hoverinfo='x+y',mode='lines',line=dict(color='rgb(172,89,200)',width=.6),stackgroup='one'))
fig.add_trace(go.Scatter(x=stud,y=[50,5,2],hoverinfo='x+y',mode='lines',line=dict(color='rgb(100,51,90)',width=.6),stackgroup='one'))
#pie chart using plotly
stud=['yasmeen','amira','marwa','sarah','sama','dalia']
marks=[34,56,76,87,45,90]
fig=go.Figure(data=[go.Pie(labels=stud,values=marks, pull=[0, 0,0 ,0.2, 0,0])])
fig.update_traces(textposition='inside',textinfo='percent+label')
fig.update_layout(title='students and their marks')
# matplotlip pie chart
palette_color = sns.color_palette('bright')
fig, ax = plt.subplots(figsize=(20,7))
# plotting data on chart
plt.pie(marks, labels=stud, colors=palette_color, explode=[0, 0,0 ,0.2, 0,0], autopct='%.1f%%')
# displaying chart
plt.show()
#multilevel pie chart plotly
stud=['yasmeen','amira','marwa','sarah','sama','dalia']
teacher=['ibrahim','mohamed','abduallh','wadood','hussien','kiwan']
marks=[34,56,76,87,45,90]
fig =go.Figure(go.Sunburst(
labels=["yasmeen", "sama","nancy","samar", "shreif", "abduallh", "wadood","ibrahim", "Abel", "dalia", "sara", "amira"],
parents=["", "yasmeen", "sama","sama", "yasmeen", "shreif", "shreif","shreif", "yasmeen", "yasmeen", "dalia", "yasmeen"],
values=[10, 14,11,8, 12, 10, 2,5, 6, 6, 4, 4],
))
#fig=go.Figure(go.Sunburst(labels=stud,parents=teacher,values=marks,))
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))
fig.show()
fig =go.Figure(go.Sunburst(
labels=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
))
# Update layout for tight margin
# See https://plotly.com/python/creating-and-updating-figures/
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))
fig.show()
#scatter plot
iris=sns.load_dataset('iris')
iris.head()
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
fig=px.scatter(iris,x='petal_length',y='petal_width',color='species',size='sepal_width',hover_data=['sepal_length'],title='Scatter plot of iris dataset')
fig.show()
fig, ax1 = plt.subplots(figsize=(15, 7))
sns.scatterplot(
data=iris, x="petal_length", y="petal_width", hue="species",sizes=(40, 400),size="sepal_width"
)
<AxesSubplot:xlabel='petal_length', ylabel='petal_width'>